package com.jvyou.condition;

/**
 * 条件工具类
 *
 * @author 橘柚
 * @since 2024/9/4 14:12
 */
public class Condition {

    /**
     * 条件判断
     *
     * @param condition 条件
     */
    public static void isTrueOrFalse(boolean condition, Runnable trueCallback, Runnable falseCallback) {
        if (condition) {
            trueCallback.run();
        } else {
            falseCallback.run();
        }
    }

    /**
     * 条件判断，条件为真时候执行回调
     *
     * @param condition 条件
     * @param callback  回调
     */
    public static void isTrue(boolean condition, Runnable callback) {
        if (condition) {
            callback.run();
        }
    }

    /**
     * 条件判断，条件为假时候执行回调
     *
     * @param condition 条件
     * @param callback  回调
     */
    public static void isFalse(boolean condition, Runnable callback) {
        isTrue(!condition, callback);
    }


    /**
     * 条件判断，为真就抛出异常
     *
     * @param condition 条件
     * @param exception 异常
     */
    public static void throwIfTrue(boolean condition, RuntimeException exception) {
        isTrue(condition, () -> {
            throw exception;
        });
    }

    /**
     * 条件判断，为假就抛出异常
     *
     * @param condition 条件
     * @param exception 异常
     */
    public static void throwIfFalse(boolean condition, RuntimeException exception) {
        isFalse(condition, () -> {
            throw exception;
        });
    }
}